﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.BasicList
{
    public partial class frm_Add_IQ : Form
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        private string error;

        public frm_Add_IQ()
        {
            InitializeComponent();

            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.error = null;
            LoadUnitList();
        }


        /////////////////////////
        /// Load Unit List
        /////////////////////////
        private void LoadUnitList()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT UnitID, UnitNameEN FROM [222_Unit] ORDER BY UnitNameEN";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["UnitID"].ToString();
                    item.Text = dataReader["UnitNameEN"].ToString();
                    this.unitID.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show("Cannot get unit list");
            }
        }       

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            error = null;

            string iq_id = null;
            string vietnam_name = null;
            string english_name = null;
            DateTime iq_date = new DateTime();
            string profile_code = null;
            string quantity = null;
            int qty = 0;
            string unit = null;
            int nvlc = 0;
            int export = 0;
            int limit = 0;

            ComboboxItem item = new ComboboxItem();

            if (string.IsNullOrEmpty(this.iqID.Text))
            {
                error = "Please enter IQ ID";
            }
            else
            {
                iq_id = this.iqID.Text;
            }

            if (string.IsNullOrEmpty(error))
            {
                if (string.IsNullOrEmpty(this.iqName.Text))
                {
                    error = "Please enter IQ Name";
                }
                else
                {
                    english_name = this.iqName.Text;
                }
            }
            
            if (string.IsNullOrEmpty(error))
            {
                if (this.iqDate.EditValue == null)
                {
                    error = "Plase select IQ Date";
                }
                else
                {
                    iq_date = DateTime.Parse(this.iqDate.EditValue.ToString());
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                if (!string.IsNullOrEmpty(this.quantityValue.Text))
                {
                    if (Int32.TryParse(this.quantityValue.Text, out qty))
                    {
                        quantity = this.quantityValue.Text;
                    }
                    else
                    {
                        error = "Invalid Quantity";
                    }
                }
                else
                {
                    quantity = "NULL";
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                vietnam_name = this.vietnamsesName.Text;
                profile_code = this.profileCode.Text;
                if (this.unitID.SelectedIndex >= 0)
                {
                    item = (ComboboxItem)this.unitID.SelectedItem;
                    unit = item.Value.ToString();
                }
                else
                {
                    unit = "No";
                }
                if (this.nvlcCheck.Checked)
                {
                    nvlc = 1;
                }
                else
                {
                    nvlc = 0;
                }
                if (this.exportCheck.Checked)
                {
                    export = 1;
                }
                else
                {
                    export = 0;
                }
                if (this.limitedCheck.Checked)
                {
                    limit = 1;
                }
                else
                {
                    limit = 0;
                }

                try
                {
                    myConnection.Open();
                    string sql = "INSERT INTO [222_IQ] ([IQID], [VietnameseName], [EnglishName], [IQDate], [ProfileCode],"
                        + " [Quantity], [UnitID], [NVLC], [ImOrEx], [Limited])"
                        + " VALUES ('" + iq_id + "', N'" + vietnam_name + "', '" + english_name + "',"
                        + " @iqdate, '" + profile_code + "', " + quantity + ", '" + unit + "',"
                        + " " + nvlc + ", " + export + ", " + limit + ")";                    
                    SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                    cmd_sql.Parameters.Add("@iqdate", SqlDbType.DateTime).Value = iq_date.Date;
                    cmd_sql.ExecuteNonQuery();
                    cmd_sql.Dispose();
                    myConnection.Close();
                    MessageBox.Show("IQ updated success.");
                    clearForm();
                }
                catch //(Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    //MessageBox.Show(ex.Message);
                    MessageBox.Show("Cannot update IQ. Please try again.");
                }
            }
            else
            {
                MessageBox.Show(error);
            }
        }

        private void clearForm()
        {
            foreach (Control c in this.Controls)
            {
                if (c is DevExpress.XtraEditors.TextEdit)
                {
                    ((DevExpress.XtraEditors.TextEdit)c).Text = null;
                }

                if (c is DevExpress.XtraEditors.ComboBoxEdit)
                {
                    ((DevExpress.XtraEditors.ComboBoxEdit)c).SelectedIndex = -1;
                }

                if (c is System.Windows.Forms.MaskedTextBox)
                {
                    ((System.Windows.Forms.MaskedTextBox)c).Text = null;
                }
            }
        }
    }
}
